﻿using System;
using System.IO;
using System.Linq;

namespace MediaAssistant.DAL
{
    public class Utility
    {
        public static string GetFileSize(long length)
        {
            string fileSize = length + " Bytes";
            double dlength = length;
            if (dlength >= 1024)
            {
                dlength = dlength / 1024;
                fileSize = String.Format("{0:#,0.##} KB", dlength);
            }
            if (dlength >= 1024)
            {
                dlength = dlength / 1024;
                fileSize = String.Format("{0:#,0.##} MB", dlength);
            }
            if (dlength >= 1024)
            {
                dlength = dlength / 1024;
                fileSize = String.Format("{0:#,0.##} GB", dlength);
            }
            return fileSize;
        }

        public static bool IsMusicFile(string fileName)
        {
            var extension = Path.GetExtension(fileName);
            return String.Compare(extension, ".mp3", StringComparison.OrdinalIgnoreCase) == 0 || String.Compare(extension, ".wma", StringComparison.OrdinalIgnoreCase) == 0;
        }

        public static bool IsMovieFile(string fileName)
        {
            var extension = Path.GetExtension(fileName);
            return SupportedMovieFileExtension.Any(se => String.Compare(extension, se, StringComparison.OrdinalIgnoreCase) == 0);
        }

        public const int HundredMB = 104857600;
        public const int MinMovieFileSize = HundredMB;
        private static readonly string[] _supportedMovieFileExtension = new[] { ".avi", ".mkv", ".flv", ".mp4", ".wmv", ".vob", ".rm", ".mpg", ".mov", ".asx", ".asf", ".3gp", ".3g2" };
        public const string SupportedFileFilter = "Music Files (*.mp3, *.wma)|*.mp3;*.wma|Movie Files (*.avi,*.mkv,*.flv,*.mp4,*.wmv,*.vob, *.rm, *.mpg, *.mov, *.asx, *.asf, *.3gp, *.3g2)|*.avi;*.mkv;*.flv;*.mp4;*.wmv;*.vob; *.rm; *.mpg; *.mov; *.asx; *.asf; *.3gp; *.3g2";
        public const string SupportedMovieFileFilter = "Movie Files (*.avi,*.mkv,*.flv,*.mp4,*.wmv,*.vob, *.rm, *.mpg, *.mov, *.asx, *.asf, *.3gp, *.3g2)|*.avi;*.mkv;*.flv;*.mp4;*.wmv;*.vob; *.rm; *.mpg; *.mov; *.asx; *.asf; *.3gp; *.3g2";

        public static bool IsSupportedMovieFile(string fileName)
        {
            var extension = Path.GetExtension(fileName);
            return SupportedMovieFileExtension.Any(e => String.Compare(e, extension, StringComparison.OrdinalIgnoreCase) == 0);
        }

        public static int GetSupportedMovieCount(string fullPath)
        {
            return SupportedMovieFileExtension.Sum(supportedExtension => GetFileCount(fullPath, supportedExtension, MinMovieFileSize));
        }

        private static int GetFileCount(string fullPath, string extension, int minFileSize)
        {
            return Directory.GetFiles(fullPath, String.Format("*{0}", extension)).Count(f => new FileInfo(f).Length > minFileSize);
        }

        public static string[] SupportedMovieFileExtension
        {
            get { return _supportedMovieFileExtension; }
        }

        
    }
}
